package com.testmanager.testmananger.quantitative.repository;


import com.querydsl.core.types.dsl.BooleanExpression;
import com.testmanager.testmananger.quantitative.constants.IteraSearchStatus;
import com.testmanager.testmananger.quantitative.constants.IterationStatus;
import com.testmanager.testmananger.quantitative.domain.IterationInfoEntity;
import com.testmanager.testmananger.quantitative.domain.QIterationInfoEntity;
import com.testmanager.testmananger.quantitative.dto.IterationInfoDto;
import com.testmanager.testmananger.quantitative.dto.IterationSearchDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional(readOnly = true)
public class IterationInfoCustom {

    @Autowired
    IterationInfoRepository iterationInfoRepository;

    public Page<IterationInfoEntity>  search(IterationSearchDto iterationSearchDto){
        QIterationInfoEntity qIterationInfoEntity=QIterationInfoEntity.iterationInfoEntity;
        BooleanExpression expression = qIterationInfoEntity.id.isNotNull();
        if (!iterationSearchDto.getStatus() .equals(IteraSearchStatus.ALL) && !iterationSearchDto.getStatus().equals(IteraSearchStatus.UNComplete)  ){
            expression = expression.and(qIterationInfoEntity.status.eq(IterationStatus.getValueOf(iterationSearchDto.getStatus().getValue()) ));
        }else if(iterationSearchDto.getStatus().equals(IteraSearchStatus.UNComplete) ){
            expression=expression.and(qIterationInfoEntity.status.in(IterationStatus.WAITDEVELOPER,IterationStatus.WAITREVIEW,
                    IterationStatus.DEVELOPING,IterationStatus.WAITTESTER,IterationStatus.TESTING,IterationStatus.WAITONLINE));
        }

        Sort orders = Sort.by(Sort.Order.asc(qIterationInfoEntity.status.getMetadata().getName()),
                Sort.Order.desc(qIterationInfoEntity.createdDate.getMetadata().getName()));
        Pageable pageable = PageRequest.of(iterationSearchDto.getPagenum(), iterationSearchDto.getSize(),orders);
       // Iterable<IterationInfoEntity> xx = iterationInfoRepository.findAll(expression);

        return iterationInfoRepository.findAll(expression, pageable);
    }

    public Page<IterationInfoEntity>  getPages(){
        QIterationInfoEntity qIterationInfoEntity=QIterationInfoEntity.iterationInfoEntity;
        BooleanExpression expression = qIterationInfoEntity.id.isNotNull();
        Sort orders = Sort.by(Sort.Order.asc(qIterationInfoEntity.status.getMetadata().getName()),
                Sort.Order.desc(qIterationInfoEntity.createdDate.getMetadata().getName()));
        Pageable pageable = PageRequest.of(0, 30,orders);
        return iterationInfoRepository.findAll(expression, pageable);
    }

}
